<template>
	<view class="con">

		<view class="logo">
			<image src="../../static/logo.png" mode="widthFix"></image>
		</view>

		<view class="form">
			<form @submit="formSubmit">
				<view class="form-item">
					<text class="iconfont icon-xiaoyouxi"></text>
					<input type="text" placeholder="请输入账号" name="account_num">
				</view>
				<view class="form-item">
					<text class="iconfont icon-xiaoyouxi"></text>
					<input type="password" placeholder="请输入密码" name="password">
				</view>
				
				<button class="login_btn" type="default" form-type="submit">登录</button>
			</form>


			
			<button class="reg_btn" type="default" @click="tuReg">注册</button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		methods: {
			tuReg() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},

			formSubmit(e) {
				// console.log(e);
				let password = e.detail.value.password;
				let account_num = e.detail.value.account_num;

		
				
					wx.request({
						url: 'https://laijunlin.xyz/index/user/login',
						method: 'POST',
						data: {
							password,
							account_num,
						},
						header: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						success: (res) => {
							console.log(res);
							uni.showToast({
								title: res.data.msg,
								icon: 'success'
							});
							
							if (res.data.msg == "登录成功") {
								// getApp().globalData.userSession = res.data.data;
								
								// 放到本地缓存里面
								uni.setStorage({
									key: 'userSession',
									data: res.data.data,
									success: function () {
										console.log('用户信息已加入缓存');
									}
								});
								
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/profile/profile'
									})
								}, 1500)
							}
						},
						fail: err => {
							console.log(err);
						}
					});

			}
		}
	}
</script>

<style scoped lang=scss>
	.con {
		padding: 0 30rpx;
	}

	.logo {
		width: 100%;
		display: flex;
		justify-content: center;
		margin-top: 60rpx;

		image {
			width: 80%;
		}
	}

	.form {
		margin-top: 60rpx;

		.login_btn,
		.reg_btn {
			width: 60%;
			background-color: #4d89b6;
			color: #fff;
			margin-top: 60rpx;
			border-radius: 20px;

		}

		.reg_btn {
			background-color: red;
		}
	}

	.form-item {
		width: 80%;
		display: flex;
		border-bottom: 1px solid #999;
		margin: 0 auto 30rpx;

		.iconfont {
			width: 100rpx;
			height: 100rpx;
			text-align: center;
			line-height: 100rpx;
			font-size: 36rpx;
		}

		input {
			flex: 1;
			height: 100rpx;
			box-sizing: border-box;
			padding-left: 10rpx;
		}

		.code {
			width: 200rpx;
			height: 80rpx;

			image {
				width: 200rpx;
				height: 80rpx;
			}
		}
	}
</style>
